package org.swingDemo.UI;

import com.esotericsoftware.tablelayout.swing.Table;
import com.formdev.flatlaf.FlatIntelliJLaf;
import org.swingDemo.swingWorker.GeneratorWorker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class MainFrame extends JFrame implements PropertyChangeListener,  WindowListener {

    public static MainFrame mainFrame;

    private ConfigPanel configPanel;
    private JButton generateButton;
    private JTextField resultField;

    public MainFrame() {
        super("彩票生成器");
        mainFrame = this;

        try {
            UIManager.setLookAndFeel(new FlatIntelliJLaf());

            initFrame();
            this.addWindowListener(this);
            this.setIconImage(new ImageIcon(MainFrame.class.getResource("/static/frame.png")).getImage());
            this.setMinimumSize(new Dimension(800, 400));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        this.setVisible(false);
        this.dispose();
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("result")) {
            String value = evt.getNewValue().toString();
            resultField.setText(resultField.getText() + " " + value);
        }
    }

    private void initFrame() {
        configPanel = new ConfigPanel();
        generateButton = new JButton("生成");
        generateButton.addActionListener(listener -> {
            handleGenerate();
        });
        resultField = new JTextField("");
        resultField.setEditable(false);

        Table pageStartTable = new Table();
        pageStartTable.addCell(configPanel).expandX().fillX();

        Table centerTable = new Table();
        centerTable.addCell(resultField).expandX().fillX();
        centerTable.row();
        centerTable.addCell(generateButton);

        this.setLayout(new BorderLayout());
        this.add(pageStartTable, BorderLayout.PAGE_START);
        this.add(centerTable, BorderLayout.CENTER);
    }

    private void handleGenerate() {
        int count = configPanel.getCount();
        if (count == -1) {
            return;
        }
        int ticketType = configPanel.getTicketType();
        if (ticketType == -1) {
            return;
        }

        resultField.setText("");
        SwingWorker swingWorker = new GeneratorWorker(count, ticketType);
        swingWorker.addPropertyChangeListener(this);
        swingWorker.execute();
    }
}
